const { MongoClient } = require('mongodb');  
const { param } = require('./routes');
  
// MongoDB连接字符串  
const url = 'mongodb://mongoAdmin:123456@web.anzhitek.com:27017/';  
// 数据库名称  
const dbName = 'sensor';  
// 集合名称  
const collectionName = 'class';  
  
// 要插入的文档  
const document = { chipid:"777" ,name:"oxygen",sensorid:"04",funid:"03",regid:"00000001",param:"0014"};
  
async function run() {  
    let client;  
    try {  
        // 连接到MongoDB  
        client = await MongoClient.connect(url);  
        console.log("Connected successfully to server");  
  
        // 选择数据库和集合  
        const db = client.db(dbName);  
        const collection = db.collection(collectionName);  
  
        // 插入文档  
        const result = await collection.insertOne(document);  
        console.log(`Inserted document with _id: ${result.insertedId}`);  
  
        // 查询文档  
        // const query = {age:18};  
        // const documents = await collection.find(query).toArray();  
        // console.log(documents); // 处理查询结果  
    } catch (err) {  
        console.error(err);  
    } finally {  
        // 关闭数据库连接  
        await client?.close();  
    }  
}  
  
run().catch(console.dir);